package com.example.myapplication;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.InputStream;
import java.net.HttpURLConnection;

import java.net.URL;

public class MainActivity extends Activity {

    private final String PATH = "http://bos.pgzs.com/rbpiczy/Wallpaper/2011/9/16/1fa20ffee11b4ae299b435945cc7e07e-10.jpg";

    private final int SUCCESS = 200;

    private final int ERROR = 404;

    private ImageView imageView;

    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.Button_1);
        imageView = findViewById(R.id.image1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getImage();
            }
        });
    }

    private ProgressDialog progressDialog = null;

    /**
     * 定义Handler，用于处理子线程发过来的信息
     */
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SUCCESS:
                    imageView.setImageBitmap((Bitmap) msg.obj);
                    break;
                case ERROR:
                    Toast.makeText(MainActivity.this, "下载失败,请检查原因", Toast.LENGTH_LONG).show();
                    break;
            }

            if (progressDialog != null)
                progressDialog.dismiss();
        }
    };


    public void getImage() {
        progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setMessage("正在下载...");
        progressDialog.show();

        // 开启子线程 下载图片
        /**
         * 执行此方法才去启动线程下载
         */
        new Thread(new DownloadImage()).start();
    }

    class DownloadImage implements Runnable {

        /**
         * 发送Handler
         */
        public void showUiImage(int responseCode, Bitmap bitmap) {
            Message message = mHandler.obtainMessage(responseCode);
            message.obj = bitmap;
            mHandler.sendMessageDelayed(message, 2000);
        }

        @Override
        public void run() {
            HttpURLConnection connection = null;
            try {
                // 封装成网络地址
                URL url = new URL(PATH);

                // 打开一个连接
                connection = (HttpURLConnection) url.openConnection();

                // 设置连接时长
                connection.setConnectTimeout(5000);

                // 设置请求方式
                connection.setRequestMethod("GET");


                if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    // 得到服务器返回过来的流对象
                    InputStream inputStream = connection.getInputStream();
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    showUiImage(SUCCESS, bitmap);
                } else {
                    showUiImage(ERROR, null);
                }

            } catch (Exception e) {
                e.printStackTrace();
                showUiImage(ERROR, null);
            } finally {
                if (connection != null) {
                    connection.disconnect();
                }
            }
        }
    }
}
